package org.suburban.data;
import java.util.LinkedList;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Query;
import static org.suburban.data.Direction.*;

/**
 * Class to upload/delete/update direction
 * @author Alexey Palamarchuk (slonnik@gmail.com)
 *
 */
public class DirectionRepository {
	/**
	 * save/update 'direction' info in the database
	 * @param id	- 'direction' id (key)
	 * @param name	- 'direction' human readable name
	 */
	public static void put(String id, String name)
	{
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Entity station = new Entity(TYPE, id);
		station.setProperty(ID, id);
		station.setProperty(NAME, name);
		datastore.put(station);
	}
	
	/**
	 * get list of directions
	 * @return	- array of direction keys
	 */
	public static String[] getDirections()
	{
		LinkedList<String> keys = new LinkedList<String>();
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		for(Entity entity :datastore.prepare(new Query(TYPE)).asIterable())
		{
			keys.add(entity.getProperty(ID).toString());
		}
		return keys.toArray(new String[]{});
	}
}
